package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.YzLiwu;
import com.ruoyi.system.domain.YzStudentBirthday;
import com.ruoyi.system.domain.YzStudentLiwu;
import com.ruoyi.system.service.IYzLiwuService;
import com.ruoyi.system.service.IYzStudentBirthdayService;
import com.ruoyi.system.service.IYzStudentLiwuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * 学生礼物信息Controller
 *
 * @author linpq
 * @date 2020-04-10
 */
@Controller
@RequestMapping("/system/liwu")
public class YzStudentLiwuController extends BaseController
{
    private String prefix = "system/liwu";

    @Autowired
    private IYzStudentLiwuService yzStudentLiwuService;
    @Autowired
    private IYzStudentBirthdayService yzStudentBirthdayService;
    @Autowired
    private IYzLiwuService yzLiwuService;

    @RequiresPermissions("system:liwu:view")
    @GetMapping()
    public String liwu()
    {
        return prefix + "/liwu";
    }

    /**
     * 查询学生礼物信息列表
     */
    @RequiresPermissions("system:liwu:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(YzStudentLiwu yzStudentLiwu)
    {
        startPage();
        List<YzStudentLiwu> list = yzStudentLiwuService.selectYzStudentLiwuList(yzStudentLiwu);
        return getDataTable(list);
    }

    /**
     * 员工礼物信息列表
     */
    @RequiresPermissions("system:liwu:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YzStudentLiwu yzStudentLiwu)
    {
        List<YzStudentLiwu> list = yzStudentLiwuService.selectYzStudentLiwuList(yzStudentLiwu);
        ExcelUtil<YzStudentLiwu> util = new ExcelUtil<YzStudentLiwu>(YzStudentLiwu.class);
        return util.exportExcel(list, "liwu");
    }

    /**
     * 新增学生礼物信息
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") String id,ModelMap mmap)
    {

        List<YzLiwu> yzLiwus = yzLiwuService.selectYzLiwuListMoreThenOne();
        mmap.put("yzLiwus",yzLiwus);

        mmap.put("delFlag",id);
        YzStudentBirthday yzStudentBirthday = yzStudentBirthdayService.selectYzStudentBirthdayById(id);
        if(yzStudentBirthday!=null)
        mmap.put("xingMing",yzStudentBirthday.getXingMing()+"-"+yzStudentBirthday.getXueXiao()+"-"+yzStudentBirthday.getNianJi()+"-"+yzStudentBirthday.getZhuanYe());


        mmap.put("songDaShiJian", DateUtils.parseDateToStr("YYYY-MM-dd", Calendar.getInstance().getTime()));

        return prefix + "/add";
    }

    /**
     * 新增学生礼物信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生礼物信息
     */
    @RequiresPermissions("system:liwu:add")
    @Log(title = "学生礼物信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YzStudentLiwu yzStudentLiwu)
    {

        YzLiwu yzLiwu = new YzLiwu();
        yzLiwu.setLiWuMingCheng(yzStudentLiwu.getLiWu());
        List<YzLiwu> yzLiwuList = yzLiwuService.selectYzLiwuList(yzLiwu);
        if(yzLiwuList!=null&&yzLiwuList.size()!=0){
            YzLiwu yzLiwu1 = yzLiwuList.get(0);
            yzLiwu1.setKuCun(yzLiwu1.getKuCun()-1);
            yzLiwuService.updateYzLiwu(yzLiwu1);
        }
        return toAjax(yzStudentLiwuService.insertYzStudentLiwu(yzStudentLiwu));
    }

    /**
     * 修改学生礼物信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        List<YzLiwu> yzLiwus = yzLiwuService.selectYzLiwuList(new YzLiwu());
        mmap.put("yzLiwus",yzLiwus);
        YzStudentLiwu yzStudentLiwu = yzStudentLiwuService.selectYzStudentLiwuById(id);
        mmap.put("yzStudentLiwu", yzStudentLiwu);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生礼物信息
     */
    @RequiresPermissions("system:liwu:edit")
    @Log(title = "学生礼物信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YzStudentLiwu yzStudentLiwu)
    {
        return toAjax(yzStudentLiwuService.updateYzStudentLiwu(yzStudentLiwu));
    }

    /**
     * 删除学生礼物信息
     */
    @RequiresPermissions("system:liwu:remove")
    @Log(title = "学生礼物信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yzStudentLiwuService.deleteYzStudentLiwuByIds(ids));
    }
}
